package com.app.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.app.biz.domain.BizSocialElectricityConsumption;
import com.app.biz.mapper.BizProduceSaleFormalMapper;
import com.app.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizProduceSaleMapper;
import com.app.biz.domain.BizProduceSale;
import com.app.biz.service.IBizProduceSaleService;

/**
 * 重点地区产销率完成情况Service业务层处理
 * 
 * @author Zhou_S
 * @date 2023-12-27
 */
@Service
public class BizProduceSaleServiceImpl implements IBizProduceSaleService 
{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BizProduceSaleMapper bizProduceSaleMapper;

    @Autowired
    private BizProduceSaleFormalMapper bizProduceSaleFormalMapper;


    /**
     * 查询重点地区产销率完成情况
     * 
     * @param baseId 重点地区产销率完成情况主键
     * @return 重点地区产销率完成情况
     */
    @Override
    public BizProduceSale selectBizProduceSaleByBaseId(String baseId)
    {
        return bizProduceSaleMapper.selectBizProduceSaleByBaseId(baseId);
    }

    /**
     * 查询重点地区产销率完成情况列表
     * 
     * @param bizProduceSale 重点地区产销率完成情况
     * @return 重点地区产销率完成情况
     */
    @Override
    public List<BizProduceSale> selectBizProduceSaleList(BizProduceSale bizProduceSale)
    {

        return bizProduceSaleMapper.selectBizProduceSaleList(bizProduceSale);
    }

    /**
     * 新增重点地区产销率完成情况
     * 
     * @param bizProduceSale 重点地区产销率完成情况
     * @return 结果
     */
    @Override
    public int insertBizProduceSale(BizProduceSale bizProduceSale)
    {
        bizProduceSale.setBaseId(IdUtil.simpleUUID());
        bizProduceSale.setBaseCreateTime(new Date());
        DateTime datesTimes= DateUtil.parse(bizProduceSale.getBaseMonth(),"yyyy年MM月");
        bizProduceSale.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizProduceSaleMapper.insertBizProduceSale(bizProduceSale);
    }

    /**
     * 修改重点地区产销率完成情况
     * 
     * @param bizProduceSale 重点地区产销率完成情况
     * @return 结果
     */
    @Override
    public int updateBizProduceSale(BizProduceSale bizProduceSale)
    {
        return bizProduceSaleMapper.updateBizProduceSale(bizProduceSale);
    }

    /**
     * 批量删除重点地区产销率完成情况
     * 
     * @param baseIds 需要删除的重点地区产销率完成情况主键
     * @return 结果
     */
    @Override
    public int deleteBizProduceSaleByBaseIds(String[] baseIds)
    {
        return bizProduceSaleMapper.deleteBizProduceSaleByBaseIds(baseIds);
    }

    /**
     * 删除重点地区产销率完成情况信息
     * 
     * @param baseId 重点地区产销率完成情况主键
     * @return 结果
     */
    @Override
    public int deleteBizProduceSaleByBaseId(String baseId)
    {
        return bizProduceSaleMapper.deleteBizProduceSaleByBaseId(baseId);
    }

    /**
     * <p>
     * 查询APP左上角的日期数据
     * </p>
     *
     * @return 日期的集合
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizProduceSaleMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            List<String> collect = uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
            collect = collect.stream().map(item -> {
                String[] split = item.split("-");
                return split[0] + "年" + split[1] + "月";
            }).collect(Collectors.toList());
            return collect;
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * <p>
     * 清空所有的重点地区产销率完成情况数据
     * </p>
     *
     * @return
     */
    @Override
    public void clearAllTableData() {
        bizProduceSaleMapper.clearAllTableData();
    }

    /**
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try{
            List<BizProduceSale> result = bizProduceSaleMapper.selectBizProduceSaleAllData();

            int count = 0;
            List<BizProduceSale> list = new ArrayList<>();
            for(BizProduceSale item : result){
                list.add(item);
                count++;
                if(count >= 100){
                    bizProduceSaleFormalMapper.batchInsertBizProduceSaleFormal(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizProduceSaleFormalMapper.batchInsertBizProduceSaleFormal(list);
                list.clear();
            }
            return true;
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return false;
        }
    }
}
